package com.example.asa;


import com.example.asa.Alert;
import com.example.asa.DatabaseUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AlertDAO {
    public List<Alert> getAllAlerts() throws SQLException {
        List<Alert> alerts = new ArrayList<>();
        String sql = "SELECT * FROM alerts ORDER BY publish_time DESC";

        try (Connection conn = DatabaseUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Alert alert = new Alert();
                alert.setId(rs.getInt("id"));
                alert.setTitle(rs.getString("title"));
                alert.setContent(rs.getString("content"));
                alert.setSeverity(rs.getString("severity"));
                alert.setPublishTime(rs.getTimestamp("publish_time"));
                alert.setPublisher(rs.getString("publisher"));

                alerts.add(alert);
            }
        }

        return alerts;
    }
}
